define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wddj/user/wechat/index' + location.search,
                    passenger_url: 'wddj/user/passenger/index',
                    emergency_contact_url: 'wddj/user/emergency_contact/index',
                    order_url: 'wddj/order/index',
                    details_url: 'wddj/user/wechat/details',
                    certification_status_url: 'wddj/user/user_real_name_authentication/details',
                    table: 'wddj_user_wechat',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'source', title: __('Source'), searchList: {"1":__('Source 1')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,buttons:[
                                {
                                    name: 'passenger',
                                    text:'常用乘车人',
                                    title: '常用乘车人',
                                    classname: 'btn btn-xs btn-primary btn-info btn-view btn-dialog bg-blue-active',
                                    url: 'wddj/user/passenger/index?wechat_id={id}',
                                    refresh:true,
                                },
                                {
                                    name: 'emergency_contact',
                                    text: '紧急联系人',
                                    title: '紧急联系人',
                                    classname: 'btn btn-xs btn-info btn-dialog bg-red-active',
                                    url: 'wddj/user/emergency_contact/index?wechat_id={id}',
                                    refresh:true,
                                },
                                {
                                    name: 'order',
                                    text: '消费记录',
                                    title: '消费记录',
                                    classname: 'btn btn-xs btn-info btn-dialog bg-olive',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'wddj/order/index?wechat_id={id}',
                                    refresh:true,
                                },
                                {
                                    name: 'details',
                                    text: '用户详情',
                                    title: '查看详情',
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa',
                                    url: 'wddj/user/wechat/details',
                                },
                                {
                                    name: 'certification_status',
                                    text: '实名认证信息',
                                    title: '查看实名认证信息',
                                    classname: 'btn btn-xs btn-info btn-dialog bg-orange-active',
                                    icon: 'fa',
                                    url: 'wddj/user/user_real_name_authentication/details?wechat_id={id}',
                                    visible:function(row){
                                        if(row['certification_status']== 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        details: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $('body').on('click', '[data-tips-image]', function () {
                    var img = new Image();
                    var imgWidth = this.getAttribute('data-width') || '480px';
                    img.onload = function () {
                        var $content = $(img).appendTo('body').css({background: '#fff', width: imgWidth, height: 'auto'});
                        Layer.open({
                            type: 1, area: imgWidth, title: false, closeBtn: 1,
                            skin: 'layui-layer-nobg', shadeClose: true, content: $content,
                            end: function () {
                                $(img).remove();
                            },
                            success: function () {

                            }
                        });
                    };
                    img.onerror = function (e) {

                    };
                    img.src = this.getAttribute('data-tips-image') || this.src;
                });
            }
        }
    };
    return Controller;
});
